﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using BenevolentSunData;

namespace BenevolentSun
{
    public class Selector
    {

        #region Graphics
        public Model Model;

        public Material material;

        Matrix[] boneTransforms;
        #endregion


        #region State
        public Point Coords = new Point();

        public Vector3 Scale = new Vector3(1f, 1f, 1f);

        private bool isGrowing = true;
        #endregion


        public Selector(Model model, Material material, Point coords)
        {
            Model = model;
            this.material = material;
            Coords = coords;

            boneTransforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(boneTransforms);
        }

        public void Update(GameTime gameTime)
        {
            float speed = (float)(gameTime.ElapsedGameTime.TotalSeconds / 3);

            if (isGrowing)
            {
                if (Scale.X >= 1.0)
                {
                    isGrowing = false;
                }
                Scale += new Vector3(speed,
                                     speed*2,
                                     speed);
            }
            else
            {
                if (Scale.X <= 0.95)
                {
                    isGrowing = true;
                }
                Scale -= new Vector3(speed,
                                     speed*2,
                                     speed);
            }
            //gameTime.ElapsedGameTime.TotalSeconds;
        }


        public void DrawEx(Vector3 position, Camera camera)
        {
            foreach (ModelMesh mesh in Model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World =
                        boneTransforms[mesh.ParentBone.Index] *
                        Matrix.CreateScale(Scale) *
                        Matrix.CreateTranslation(position);
                    effect.Projection = camera.Proj;
                    effect.View = camera.View;

                    effect.SpecularPower = material.SpecularPower;
                    effect.SpecularColor = material.SpecularColor.ToVector3();
                    effect.DiffuseColor = material.DiffuseColor.ToVector3();
                    effect.Alpha = material.Alpha;
                    effect.EmissiveColor = material.EmissiveColor.ToVector3();
                }

                mesh.Draw();
            }
        }

        public void Draw(ref GraphicsDevice graphics, ref Matrix view, ref Matrix proj, Vector3 position)
        {
            foreach (ModelMesh mesh in Model.Meshes)
            {
                DrawMesh(mesh, ref proj, ref view, ref position);
            }
        }

        protected void DrawMesh(ModelMesh mesh, ref Matrix proj, ref Matrix view, ref Vector3 position)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                effect.EnableDefaultLighting();
                effect.PreferPerPixelLighting = true;
                //effect.FogEnabled = true;
                //effect.FogColor = new Vector3(0.0f, 0.0f, 0.0f);
                //effect.FogStart = 500f;
                //effect.FogEnd = 750f;
                effect.World =
                    boneTransforms[mesh.ParentBone.Index] *
                    Matrix.CreateScale(Scale) *
                    Matrix.CreateTranslation(position);
                effect.Projection = proj;
                effect.View = view;

                //effect.AmbientLightColor = material.AmbientLightColor;
                effect.SpecularPower = material.SpecularPower;
                //effect.SpecularColor = material.SpecularColor;
                //effect.DiffuseColor = material.DiffuseColor;
                effect.Alpha = material.Alpha;
                //effect.EmissiveColor = material.EmissiveColor;
                //if (material.Texture != null)
                //{
                //    effect.TextureEnabled = true;
                //    effect.Texture = material.Texture;
                //}
                //else
                //{
                //    effect.TextureEnabled = false;
                //}
            }

            mesh.Draw();
        }

    }
}
